<?php

namespace App\Services\Common;

class RpcService
{

    public static function garage($param = [])
    {
        $param['service'] = __FUNCTION__;
        $result = self::execute($param);
        if ($result['code'] != 10000) {
            throw new \Exception($result['msg'], 0);
        }
        return $result;
    }

    public static function execute($param = [])
    {
        $service = $param['service'] ?? '';
        if (empty($service)) {
            throw new \Exception("服务名称不可为空", 0);
        }
        unset($param['service']);

        $conf = config('auth.rpc')[$service];

        $client = new \Yar_Client($conf['host']);
        $client->SetOpt(YAR_OPT_HEADER, ['token:' . $conf['token']]);
        $result = $client->execute([
            'method' => $param['method'] ?? '',
            'data' => $param['data'] ?? []
        ]);
        return $result;
    }
}
